package com.hongye.proprietary.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.hongye.proprietary.config.PathResource;
import com.hongye.proprietary.controller.vo.LoginVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.hongye.proprietary.entity.UserHomeEntity;
import com.hongye.proprietary.service.UserHomeService;
import com.hongye.common.utils.PageUtils;
import com.hongye.common.utils.R;

import javax.servlet.http.HttpSession;


/**
 * 用户表
 *
 * @author lins
 * @email 695565697@qq.com
 * @date 2021-10-14 11:16:47
 */
@RestController
@RequestMapping("proprietary/userhome")
public class UserHomeController {
    @Autowired
    private UserHomeService userHomeService;
    @Autowired
    private PathResource pathResource;

    @RequestMapping("/aa")
    public void ss(HttpSession session){
        Object userlogin = session.getAttribute("userlogin");
        String s = JSON.toJSONString(userlogin);
        UserHomeEntity userHomeEntity = JSON.parseObject(s, new TypeReference<UserHomeEntity>(){});
        Integer userId = userHomeEntity.getUserId();
        System.out.println(userId);
    }
    //客户端登录
    // 验证码登录
    @RequestMapping("/login")
    public R sendMsg(@RequestBody LoginVo loginVo,HttpSession session){
        return userHomeService.login(loginVo);
    }
    /*
    * 客户端发送验证码
    * */
    @RequestMapping("/sendMsg")
    public R sendMsg(String userPhone){
        //手机号非空
        if (pathResource.isNullNO(userPhone)){
            //判断手机号格式
            if (pathResource.isMobileNO(userPhone)){
                return userHomeService.sendMsg(userPhone);
            }
            return R.error("手机号格式错误");
        }
        return R.error("手机号不能为空");
    }
    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = userHomeService.queryPage(params);

        return R.ok().put("page", page);
    }

    @RequestMapping("/list2")
    public List<UserHomeEntity> list2(){
        return userHomeService.list();

    }

    /**
     * 信息
     */
    @RequestMapping("/info/{userId}")
    public R info(@PathVariable("userId") Integer userId){
		UserHomeEntity userHome = userHomeService.getById(userId);

        return R.ok().put("userHome", userHome);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody UserHomeEntity userHome){
		userHomeService.save(userHome);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody UserHomeEntity userHome){
		userHomeService.updateById(userHome);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] userIds){
		userHomeService.removeByIds(Arrays.asList(userIds));

        return R.ok();
    }

}
